<?php

namespace apiend\modules\work\controllers;

use Yii;
use apiend\components\Controller;
use apiend\modules\work\models\StaffForm;
use apiend\modules\work\models\StaffSearch;

/**
 * 人员管理接口
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class StaffController extends Controller {

    /**
     * @inheritdoc
     */
    protected function rules() {
        return [
            [
                'allow' => true,
                'roles' => ['@'],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public $modelClass = StaffSearch::class;

    /**
     * 人员管理
     * @return mixed
     */
    public function actionIndex() {
        /* @var $model StaffSearch */
        $model = new $this->modelClass;
        return $model->configs();
    }

    /**
     * 人员管理
     * @return mixed
     */
    public function actionList() {
        /* @var $model StaffSearch */
        $model = new $this->modelClass;
        $model->load(Yii::$app->request->getQueryParams(), '');
        return $model->search();
    }

    /**
     * 人员显示
     * @return mixed
     */
    public function actionView($id) {
        return SurveyViewSearch::findOrFail($id);
    }

    /**
     * 维护人员
     * @return mixed
     */
    public function actionUpdate($id = null) {
        $model = new StaffForm($id);
        if (Yii::$app->request->isPost) {
            $params = Yii::$app->request->post();
            if (!$model->load($params, '')) {
                parent::error('数据提交失败');
            }
            return $model->save();
        }
        return $model;
    }

    /**
     * 删除人员
     * @return mixed
     */
    public function actionDelete($id) {
        $model = StaffSearch::findOrFail($id);
        if ($model->user_id !== $this->uid) {
            parent::error('非法请求！');
        }
        if (!$model->delete()) {
            parent::error('人员删除失败');
        }
        return true;
    }

}
